< previous page page_458 next page >

Page 458
Chapter Objectives
Learn to control user input
Learn to use the standard controls for validation
Learn about formatted input
Learn about third-party controls
Learn the new functions in Visual Basic 6
Sample Project File: \Chapter23\Tips.vbp
Handling Keystrokes
Regardless of the type of validation routines you use after the user has entered data, sometimes its desirable to validate the keystrokes as the user enters them. Although theres a custom control called the Masked Edit control that allows you to set the format for valid characters, its sometimes better to trap the keystrokes yourself. Lets look at how to handle keystrokes for a text box control (see Figure 23.1).
Examples Filename: Keystrokes.frm
8160-0458a.gif
Figure 23.1
Trapping keystrokes in a sample form.
Uppercase Input
To make all characters uppercase in a text box or other input control, all you need to do is add one line of code to the KeyPress event. The KeyPress event is passed the key thats pressed prior to that key being entered into the text box. The keystroke is passed in as an ASCII character value. This means you need to convert it to a normal character using the Chr$() function and then apply the UCase$() function to the result. Next, you need to convert it back to ASCII code using the Asc() function. The resulting ASCII code is then assigned back to the KeyAscii argument. Because the KeyAscii argument is passed by reference, any changes to this key value will be placed into the text box:

 
< previous page page_458 next page >

If you like this book, buy it!